﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication1.DesignerPatter.Flyweight
{
    public class Program
    {
        public static void Main()
        {
            WebsiteFactory wf = new WebsiteFactory();
            WebSite fx = wf.getWebSiteFactory("产品展示");
            fx.Use(new User("小菜"));

            WebSite fy = wf.getWebSiteFactory("电子商务");
            fy.Use(new User("喵喵"));

            Console.WriteLine("得到网站分类总数:"+wf.GetWebiteCount());
            Console.Read();
        }
    }

    #region 用户类
    public class User
    {
        private string name;
        public User(string name)
        {
            this.name = name;
        }

        public string Name
        {
            get { return name; }
        }
    }

    #endregion

    #region 网站抽象类
    public abstract class WebSite
    {
        public abstract void Use(User user);
    }
    #endregion

    #region 网站具体类

    class ConcreteWebSite : WebSite
    {
        private string name = "";

        public ConcreteWebSite(string name)
        {
            this.name = name;
        }

        public override void Use(User user)
        {
            Console.WriteLine(string .Format("网站分类:{0},用户:{1}",name,user.Name));
        }
    }
    #endregion

    #region 网站工厂类

    public  class WebsiteFactory
    {
        Hashtable ht = new Hashtable();

        public WebSite getWebSiteFactory(string key)
        {
            if (!ht.Contains(key))
            {
                ht.Add(key, new ConcreteWebSite(key));
            }

            return((WebSite) ht[key]);
        }

        public int GetWebiteCount()
        {
            return ht.Count;
        }
    }

    #endregion
}
